<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-for讲解</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
    <h1 >v-for讲解</h1>
    <hr>
    <div id="app">
        <ul>
            <li v-for="item in sortItems">
                {{item}}
            </li>
        </ul>
        <hr>
        <ul>
            <li v-for="(student,index) in sortStudents">
               {{index+1}} : {{student.name}}-{{student.age}}
            </li>
        </ul>
        </div>
    <script type="text/javascript">
        var aaa = new Vue({
            el:'#app',
            data:{
                items:[12,43,545,65,43,54,345],
                students:[
                    {name:"bossli" , age:26},
                    {name:"李兵" , age:18},
                    {name:"李红" , age:20}
                ]
            },
            
            computed:{
                //基本数据类型数组排序
                sortItems:function(){
                    return this.items.sort( sortBy);
                },
                // 引用数据类型数组排序
                sortStudents:function(){
                    return sortByKey(this.students,"age");
                }
            }
        });
        //数组基本数据类型排序
        function sortBy(a,b){
            return a-b;
        }

        //数组对象方法排序:
        function sortByKey(array,key){
            return array.sort(function(a,b){
            var x=a[key];
            var y=b[key];
            return ((x<y)?-1:((x>y)?1:0));
            });
        }
    </script>
    
</body>
</html>